<template>
  <div>
    <el-row>
      <h3 style="color:#409EFF">如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)？</h3>
      <el-table
        :data="tableData1"
        style="width: 100%"
        stripe
        max-height=800>
        <el-table-column
          prop="lan"
          label="语言"
          width="200">
        </el-table-column>
        <el-table-column
          prop="code"
          label="代码">
        </el-table-column>
      </el-table>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'Unixtime',
  data () {
    return {
      input: '',
      result: '',
      tableData1: [{
        lan: 'Java',
        code: 'time'
      }, {
        lan: 'JavaScript',
        code: 'Math.round(new Date().getTime()/1000)\ngetTime()返回数值的单位是毫秒'
      }, {
        lan: 'PHP',
        code: 'time()'
      }, {
        lan: 'MySQL',
        code: 'SELECT unix_timestamp(now())'
      }, {
        lan: 'Microsoft .NET / C#',
        code: 'epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000'
      }, {
        lan: 'Perl',
        code: 'time'
      }, {
        lan: 'PostgreSQL',
        code: 'SELECT extract(epoch FROM now())'
      }, {
        lan: 'Python',
        code: '先 import time 然后 time.time()'
      }, {
        lan: 'Ruby',
        code: '获取Unix时间戳：Time.now 或 Time.new\n显示Unix时间戳：Time.now.to_i'
      }, {
        lan: 'SQL Server',
        code: 'SELECT DATEDIFF(s, \'1970-01-01 00:00:00\', GETUTCDATE())'
      }, {
        lan: 'Unix / Linux',
        code: 'date +%s'
      }, {
        lan: 'VBScript / ASP',
        code: 'DateDiff("s", "01/01/1970 00:00:00", Now())'
      }, {
        lan: '其他操作系统\n(如果Perl被安装在系统中)',
        code: '命令行状态：perl -e "print time"'
      }]
    }
  },
  methods: {
    ChiToUtf8 () {
      if (!this.input) {
        this.openAlert('文本框不能为空')
        return
      }
      // eslint-disable-next-line
      this.result = this.input.replace(/[^\u0000-\u00FF]/g, function ($0) {
        return escape($0).replace(/(%u)(\w{4})/gi, '&#x$2;')
      })
    },
    Utf8ToChi () {
      if (!this.result) {
        this.openAlert('文本框不能为空')
        return
      }
      this.input = unescape(this.result.replace(/&#x/g, '%u').replace(/\\u/g, '%u').replace(/;/g, ''))
    },
    reset () {
      this.input = ''
      this.result = ''
    },
    // 弹框提示
    openAlert (msg) {
      this.$alert(msg, '错误提示', {
        confirmButtonText: '确定',
        callback: action => {}
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.el-input {
  padding: 10px
}
.el-table .warning-row {
  background: oldlace;
}
.el-table .success-row {
  background: #f0f9eb;
}
</style>
